package com.neuedu.security.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;

import com.neuedu.security.common.Ret;
import com.neuedu.security.entity.SysUser;
import com.neuedu.security.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 用户信息表 前端控制器
 * </p>
 *
 * @author 金山老师
 * @since 2023-03-15
 */
@RestController
@RequestMapping("/sysUser")
@Slf4j
@Api(tags = "用户管理")
public class SysUserController {

    @Autowired
    ISysUserService sysUserService;


    /**
     * sysUser.setUserName("root");
     * sysUser.setNickName("超级管理员");
     * sysUser.setUserType("00");
     * sysUser.setEmail("zhangjinshan@neuedu.com");
     * http://127.0.0.1:8080/sysUser/save?userName=myroot&nickName=测试管理员&email=zbc@aqa.com
     *
     * @param user
     * @return
     */
    @ApiOperation(value = "用户保存")
    @PostMapping("/saveOrUpdate")
    public boolean saveOrUpdate(@RequestBody SysUser user) {

        System.out.println(user);
        log.debug(user.toString());

        return sysUserService.saveOrUpdate(user);

    }


    /**
     *  com.neuedu.mp.controller.SysUserController.getById=sysuser:getById
     *
     *  @AOPAuthorization(menuList = "system:user:query")  期望访问Controller有 system:user:query
     * @param id
     * @return
     */
    @ApiOperation(value = "根据主键查询用户")
    @GetMapping("/getById/{id}")
    public Ret<SysUser> getById(@PathVariable Integer id) {
        return Ret.ok( sysUserService.getById(id));
    }



    @ApiOperation(value = "用户列表查询")
    @GetMapping("/list")
    @ApiImplicitParams(
            value={
                    @ApiImplicitParam(name="nickName" ,value="昵称")
            }
    )
    public List<SysUser> list(String nickName) {


        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<SysUser>();
        if(nickName != null){
            //        // like
            queryWrapper.like(SysUser::getNickName, nickName);
        }

        return sysUserService.list(queryWrapper);

    }


    @ApiOperation(value = "用户列表分页查询")
    @GetMapping("/page")
    @ApiImplicitParams(
            value={
                    @ApiImplicitParam(name="nickName" ,value="昵称")
            }
    )
    public Page<SysUser> page(Page<SysUser> page, String nickName) {


        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<SysUser>();
        if(nickName != null){
            queryWrapper.like(SysUser::getNickName, nickName);
        }

        sysUserService.page(page,queryWrapper);

        return page;
    }


    @ApiOperation(value = "用户根据主键删除")
    @GetMapping("/removeById/{id}")
    public boolean removeById(@PathVariable long id) {
        return sysUserService.removeById(id);

    }





//    @ApiOperation(value = "给用户授权角色")
//    @PostMapping("/grant")
//    public boolean grant(@RequestBody UserRoleGrant grant ) throws JsonProcessingException {
//
//        return sysUserService.grant(grant.getUserId(),grant.getRoleIds());
//
//
//    }


}
